/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.attachment.controller  
 */
package com.apache.attachment.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.action.PlatFromAction;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.LoggerVo;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.attachment.entity.AttachmentRegister;
import com.apache.attachment.manager.AttachmentRegisterManager;
import com.apache.attachment.mommon.BeanUtil;
import com.apache.cache.util.Validator;

/**
 * @author mhguo 创建时间：2015-5-12  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
@Controller
@RequestMapping("/attachment/register/")
public class AttachmentRegisterAction extends PlatFromAction<AttachmentRegister> {

	/**
	 * 附件库注册管理的Service
	 */
	@Autowired
	AttachmentRegisterManager attachmentRegisterManager;

	/**
	 *  初始化信息，跳转到新增页面  
	 */
	@Token(save = true)
	@RequestMapping("add.action")
	protected ModelAndView add(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(this.ADD);
		return view;
	}

	/**
	 * 初始化信息，跳转到修改页面  
	 */
	@Token(save = true)
	@RequestMapping("edit.action")
	public ModelAndView edit(HttpServletRequest request, String id) {
		ModelAndView view = new ModelAndView(EDIT);
		AttachmentRegister register = null;
		ParamsVo<AttachmentRegister> vo = new ParamsVo<AttachmentRegister>();
		vo.setInfoId(id);
		register = (AttachmentRegister) attachmentRegisterManager.getInfoById(vo);
		view.addObject("register", register);
		return view;
	}

	/**
	 * 新增、修改附件库注册信息 
	 */
	@Token(remove = true)
	@RequestMapping("save.action")
	public @ResponseBody
	ResultMsg save(HttpServletRequest request, HttpServletResponse response, AttachmentRegister register) {
		//初始化结果提示信息
		ResultMsg rmsg = new ResultMsg();
		rmsg.setFlag("T");
		rmsg.setMsg("操作成功！");
		response.setContentType("text/html");
		String rid = register.getRid();//注册id

		LoggerVo loggerVo = new LoggerVo();
		ParamsVo<AttachmentRegister> vo = this.getParamsVo(request);//调用父类方法得到初始化过的ParamsVo对象

		if (Validator.isNull(rid)) {//新增

			//检查注册英文标示是否被占用
			ParamsVo<AttachmentRegister> pvo = new ParamsVo<AttachmentRegister>();
			pvo.setMethodKey("checkUniqByREname");
			pvo.setParams("regEname", register.getRegEname());

			String pregEname = String.valueOf(pvo.getParams("regEname"));
			if (Validator.isNull(pregEname)) {
				return getResultMsg(request, "缺少必要的参数!");
			}

			Object obj = attachmentRegisterManager.execute(pvo);

			if ("true".equals(obj.toString())) {
				return getResultMsg(request, "注册英文标示已经被占用了!");

			}

			vo.setObj(register);
			rid = attachmentRegisterManager.saveInfo(vo);
			if (Validator.isNull(rid)) {
				return rmsg = getResultMsg(request, "新增条目失败，请重试!");
			}
		} else {//修改

			ParamsVo<AttachmentRegister> pvo = new ParamsVo<AttachmentRegister>();
			pvo.setMethodKey("checkUniqByREnameAndRid");
			pvo.setParams("regEname", register.getRegEname());
			pvo.setParams("rid", rid);

			String pregEname = String.valueOf(pvo.getParams("regEname"));
			String prid = String.valueOf(pvo.getParams("rid"));
			if (Validator.isNull(pregEname) || Validator.isNull(prid)) {
				return getResultMsg(request, "缺少必要的参数!");
			}

			Object obj = attachmentRegisterManager.execute(pvo);

			if ("true".equals(obj.toString())) {
				return getResultMsg(request, "注册英文标示已经被占用了!");
			}
			vo.setInfoId(rid);
			AttachmentRegister regEntity = (AttachmentRegister) attachmentRegisterManager.getInfoById(vo);
			vo.setInfoId(null);
			BeanUtil.copy(register, regEntity);//如果register属性值为null,不覆盖regEntity
			vo.setObj(regEntity);
			boolean mark = attachmentRegisterManager.editInfo(vo);
			if (!mark) {
				rmsg.setFlag("F");
				rmsg.setMsg("操作失败！");
			}
		}

		return rmsg;
	}

	ResultMsg getResultMsg(HttpServletRequest request, String msg) {
		request.getSession().setAttribute("formToken", request.getAttribute("formToken"));
		ResultMsg rmsg = new ResultMsg("F", msg);
		return rmsg;
	}

	/**
	 * 删除注册信息
	 */
	@RequestMapping("del.action")
	@ResponseBody
	public ResultMsg delete(HttpServletRequest request, String id) {
		//初始化结果信息
		ResultMsg rmsg = new ResultMsg();
		rmsg.setFlag("T");
		rmsg.setMsg("操作成功！");

		if (Validator.isNull(id)) {
			rmsg.setFlag("F");
			rmsg.setMsg("缺少必要的参数!");
			return rmsg;
		}

		ParamsVo<AttachmentRegister> vo = new ParamsVo<AttachmentRegister>();
		vo.setInfoId(id);

		//检验注册信息是否存在
		AttachmentRegister ar = (AttachmentRegister) attachmentRegisterManager.getInfoById(vo);
		if (Validator.isEmpty(ar)) {
			rmsg.setFlag("F");
			rmsg.setMsg("注册信息不存在或者已经删除！");
			return rmsg;
		}

		boolean mark = attachmentRegisterManager.deleteInfo(vo);
		if (!mark) {
			rmsg.setFlag("F");
			rmsg.setMsg("操作失败！");
		}
		return rmsg;
	}

	/**
	 *  根据附件库类型取得附件库注册列表信息
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("list!data.action")
	@ResponseBody
	protected Object data(HttpServletRequest request, String page, String rows) {
		ParamsVo vo = new ParamsVo();
		List<AttachmentRegister> attaList = attachmentRegisterManager.getList(vo);
		return attaList;
	}

	@Override
	protected BaseManager<AttachmentRegister> getBaseManager() {
		return attachmentRegisterManager;
	}

	@Override
	protected String getPrefix() {
		return "attachment/mvc/attachment-register-";
	}

}
